package xyz.yuzegod.megawalls.classes.normal.pirate;

import net.citizensnpcs.api.CitizensAPI;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.ProjectileLaunchEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.metadata.MetadataValue;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import xyz.yuzegod.megawalls.MegaWalls;
import xyz.yuzegod.megawalls.classes.*;
import xyz.yuzegod.megawalls.game.GamePlayer;
import xyz.yuzegod.megawalls.util.ItemBuilder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Pirate extends Classes
{
    public static final Map<GamePlayer, Integer> skill2Cooldown;
    public static final Map<GamePlayer, Integer> skill3Cooldown;
    
    public Pirate() {
        super("Pirate", "海盗", ChatColor.GOLD, Material.STONE_AXE, (byte)0, ClassesType.NORMAL, new ClassesInfo.Orientation[] { ClassesInfo.Orientation.WARRIOR, ClassesInfo.Orientation.HURT }, ClassesInfo.Difficulty.TWO);
        this.setMainSkill(new MainSkill(this));
        this.setSecondSkill(new SecondSkill(this));
        this.setThirdSkill(new ThirdSkill(this));
        this.setCollectSkill(new FourthSkill(this));
        this.setDefaultSkin("eyJ0aW1lc3RhbXAiOjE1NDk0Mzc4NDUyNzYsInByb2ZpbGVJZCI6ImRhNzQ2NWVkMjljYjRkZTA5MzRkOTIwMTc0NDkxMzU1IiwicHJvZmlsZU5hbWUiOiJLaWRTbGljZXIiLCJzaWduYXR1cmVSZXF1aXJlZCI6dHJ1ZSwidGV4dHVyZXMiOnsiU0tJTiI6eyJ1cmwiOiJodHRwOi8vdGV4dHVyZXMubWluZWNyYWZ0Lm5ldC90ZXh0dXJlL2VjMzk1Mzc0OTc5MGQ1M2NlODkwZmVlN2UzMDY5NzQwNTIwZmJiMjQyNjJmY2VlNWZjNjAwNGNmODcyMDVhZDkifX19", "sjMOCPlowVjBHyHMUaNahonctz7XZtsquJp6WEPX4m9KGjFUtBij/AYNToPpOo5liF/pngv+1hTWd4l341rY/ZC44W+S82hklku5mdprG9uSkxDyxuoFCBdGVaxzHY1rzIY++fNs1aoXFLJUtb62lxe8JA4j5VrDI/yoAREyKdqsFoazL38feeRYsnw3xXugy06PteiguQ848lGpqJDW9jCqjcxU75YBaqGiPGEPNGkTQQbfGoJkDwmQ3HCRnvOJ/YwKZ8OLzWWxmCouI1MKOztE3kao8lAwxJ3LYUyApzBRIrlPXhhh5ZrMock85rXAky2EYYvebAO03qoT9lbVLvm/CvQ57Y5jap696/V0mG4IleDW743itu2G9OBt9kqrwodn6IzQ4ln2i7C2ggIa361MCjbObH2tZ/s2Uyaa+EgzVTqGAbUY3I9nhBpWGPPiRqWgOC0Ug+T6912sZd7wyFerwmZlxSESal5fpuFBukHFJhn54XSsL0lhP2Gkl/b/5DeenvAVrTaODtCpx9K0bRnBkKjQpoEkd/AsVm3OaO6h73XL8F6QxWNW+DyovRlVvuSAr0Wzf/rIv4S9KwMsc7oLOu86mw9N8+t37y8BC48iJi9n0pfwdtZbAxVEDa3Gwmk6Ai7d/kZY6Qv55t1LM3kULBcq/elOvuvj0GBTBgk=");
        this.setEquipmentPackage(new EquipmentPackage(this) {
            @Override
            public List<ItemStack> getEquipments(final int level) {
                final List<ItemStack> items = new ArrayList<ItemStack>();
                items.add(new ItemBuilder(Material.COMPASS).setDisplayName(Pirate.this.nameColor + Pirate.this.getDisplayName() + " 指南针").build());
                items.add(new ItemBuilder(Material.DIAMOND_PICKAXE).setDisplayName(Pirate.this.nameColor + Pirate.this.getDisplayName() + " 镐").setUnbreakable(true).addEnchantment(Enchantment.DIG_SPEED, 3).addEnchantment(Enchantment.DURABILITY, 3).build());
                items.add(new ItemBuilder(Material.ENDER_CHEST).setDisplayName(Pirate.this.nameColor + Pirate.this.getDisplayName() + " 末影箱").build());
                switch (level) {
                    case 1: {
                        items.add(new ItemBuilder(Material.IRON_SWORD).setDisplayName(Pirate.this.nameColor + Pirate.this.getDisplayName() + " 剑").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).build());
                        items.add(new ItemBuilder(Material.BOW).setDisplayName(Pirate.this.nameColor + Pirate.this.getDisplayName() + " 弓").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).addEnchantment(Enchantment.ARROW_DAMAGE, 1).build());
                        items.add(new ItemBuilder(Material.IRON_LEGGINGS).setDisplayName(Pirate.this.nameColor + Pirate.this.getDisplayName() + " 护腿").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1).addEnchantment(Enchantment.PROTECTION_FIRE, 1).build());
                        items.add(new ItemBuilder(Material.POTION, 1, (byte)5).setDisplayName(Pirate.this.nameColor + Pirate.this.getDisplayName() + " 治疗药水 (8❤)").addPotion(new PotionEffect(PotionEffectType.HEAL, 1, 2)).build());
                        items.add(new ItemBuilder(Material.ARROW, 32).setDisplayName(Pirate.this.nameColor + Pirate.this.getDisplayName() + " 箭").build());
                        items.add(new ItemBuilder(Material.COOKED_BEEF).setDisplayName(Pirate.this.nameColor + Pirate.this.getDisplayName() + " 牛排").build());
                        break;
                    }
                    case 2: {
                        items.add(new ItemBuilder(Material.IRON_SWORD).setDisplayName(Pirate.this.nameColor + Pirate.this.getDisplayName() + " 剑").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).build());
                        items.add(new ItemBuilder(Material.BOW).setDisplayName(Pirate.this.nameColor + Pirate.this.getDisplayName() + " 弓").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).addEnchantment(Enchantment.ARROW_DAMAGE, 1).build());
                        items.add(new ItemBuilder(Material.IRON_LEGGINGS).setDisplayName(Pirate.this.nameColor + Pirate.this.getDisplayName() + " 护腿").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1).addEnchantment(Enchantment.PROTECTION_FIRE, 1).addEnchantment(Enchantment.PROTECTION_PROJECTILE, 1).build());
                        items.add(new ItemBuilder(Material.POTION, 1, (byte)5).setDisplayName(Pirate.this.nameColor + Pirate.this.getDisplayName() + " 治疗药水 (8❤)").addPotion(new PotionEffect(PotionEffectType.HEAL, 1, 2)).build());
                        items.add(new ItemBuilder(Material.ARROW, 56).setDisplayName(Pirate.this.nameColor + Pirate.this.getDisplayName() + " 箭").build());
                        items.add(new ItemBuilder(Material.COOKED_BEEF, 2).setDisplayName(Pirate.this.nameColor + Pirate.this.getDisplayName() + " 牛排").build());
                        items.add(new ItemBuilder(Material.POTION, 1, (byte)2).setDisplayName(Pirate.this.nameColor + Pirate.this.getDisplayName() + " 速度药水 II (0:15秒)").addPotion(new PotionEffect(PotionEffectType.SPEED, 300, 1)).build());
                        break;
                    }
                    case 3: {
                        items.add(new ItemBuilder(Material.DIAMOND_SWORD).setDisplayName(Pirate.this.nameColor + Pirate.this.getDisplayName() + " 剑").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).build());
                        items.add(new ItemBuilder(Material.BOW).setDisplayName(Pirate.this.nameColor + Pirate.this.getDisplayName() + " 弓").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).addEnchantment(Enchantment.ARROW_DAMAGE, 1).build());
                        items.add(new ItemBuilder(Material.IRON_LEGGINGS).setDisplayName(Pirate.this.nameColor + Pirate.this.getDisplayName() + " 护腿").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 2).addEnchantment(Enchantment.PROTECTION_FIRE, 2).addEnchantment(Enchantment.PROTECTION_PROJECTILE, 1).build());
                        items.add(new ItemBuilder(Material.POTION, 1, (byte)5).setDisplayName(Pirate.this.nameColor + Pirate.this.getDisplayName() + " 治疗药水 (8❤)").addPotion(new PotionEffect(PotionEffectType.HEAL, 1, 2)).build());
                        items.add(new ItemBuilder(Material.ARROW, 64).setDisplayName(Pirate.this.nameColor + Pirate.this.getDisplayName() + " 箭").build());
                        items.add(new ItemBuilder(Material.ARROW, 16).setDisplayName(Pirate.this.nameColor + Pirate.this.getDisplayName() + " 箭").build());
                        items.add(new ItemBuilder(Material.COOKED_BEEF, 2).setDisplayName(Pirate.this.nameColor + Pirate.this.getDisplayName() + " 牛排").build());
                        items.add(new ItemBuilder(Material.POTION, 2, (byte)2).setDisplayName(Pirate.this.nameColor + Pirate.this.getDisplayName() + " 速度药水 II (0:15秒)").addPotion(new PotionEffect(PotionEffectType.SPEED, 300, 1)).build());
                        break;
                    }
                    case 4: {
                        items.add(new ItemBuilder(Material.DIAMOND_SWORD).setDisplayName(Pirate.this.nameColor + Pirate.this.getDisplayName() + " 剑").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).build());
                        items.add(new ItemBuilder(Material.BOW).setDisplayName(Pirate.this.nameColor + Pirate.this.getDisplayName() + " 弓").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).addEnchantment(Enchantment.ARROW_DAMAGE, 2).build());
                        items.add(new ItemBuilder(Material.IRON_LEGGINGS).setDisplayName(Pirate.this.nameColor + Pirate.this.getDisplayName() + " 护腿").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 2).addEnchantment(Enchantment.PROTECTION_FIRE, 2).addEnchantment(Enchantment.PROTECTION_PROJECTILE, 2).build());
                        items.add(new ItemBuilder(Material.POTION, 2, (byte)5).setDisplayName(Pirate.this.nameColor + Pirate.this.getDisplayName() + " 治疗药水 (8❤)").addPotion(new PotionEffect(PotionEffectType.HEAL, 1, 2)).build());
                        items.add(new ItemBuilder(Material.ARROW, 64).setDisplayName(Pirate.this.nameColor + Pirate.this.getDisplayName() + " 箭").build());
                        items.add(new ItemBuilder(Material.COOKED_BEEF, 3).setDisplayName(Pirate.this.nameColor + Pirate.this.getDisplayName() + " 牛排").build());
                        items.add(new ItemBuilder(Material.POTION, 2, (byte)2).setDisplayName(Pirate.this.nameColor + Pirate.this.getDisplayName() + " 速度药水 II (0:15秒)").addPotion(new PotionEffect(PotionEffectType.SPEED, 300, 1)).build());
                        break;
                    }
                    case 5: {
                        items.add(new ItemBuilder(Material.DIAMOND_SWORD).setDisplayName(Pirate.this.nameColor + Pirate.this.getDisplayName() + " 剑").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).build());
                        items.add(new ItemBuilder(Material.BOW).setDisplayName(Pirate.this.nameColor + Pirate.this.getDisplayName() + " 弓").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).addEnchantment(Enchantment.ARROW_DAMAGE, 2).build());
                        items.add(new ItemBuilder(Material.IRON_LEGGINGS).setDisplayName(Pirate.this.nameColor + Pirate.this.getDisplayName() + " 护腿").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY, 10).addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 3).addEnchantment(Enchantment.PROTECTION_FIRE, 2).addEnchantment(Enchantment.PROTECTION_PROJECTILE, 2).build());
                        items.add(new ItemBuilder(Material.POTION, 2, (byte)5).setDisplayName(Pirate.this.nameColor + Pirate.this.getDisplayName() + " 治疗药水 (8❤)").addPotion(new PotionEffect(PotionEffectType.HEAL, 1, 2)).build());
                        items.add(new ItemBuilder(Material.ARROW, 64).setDisplayName(Pirate.this.nameColor + Pirate.this.getDisplayName() + " 箭").build());
                        items.add(new ItemBuilder(Material.COOKED_BEEF, 3).setDisplayName(Pirate.this.nameColor + Pirate.this.getDisplayName() + " 牛排").build());
                        items.add(new ItemBuilder(Material.POTION, 2, (byte)2).setDisplayName(Pirate.this.nameColor + Pirate.this.getDisplayName() + " 速度药水 II (0:15秒)").addPotion(new PotionEffect(PotionEffectType.SPEED, 300, 1)).build());
                        break;
                    }
                }
                if (xyz.yuzegod.megawalls.util.ItemUtils.containsBow(items)) {
                    return items;
                }else {
                    items.add(new ItemBuilder(Material.BOW).setDisplayName(nameColor+getDisplayName()+" 弓").setUnbreakable(true).addEnchantment(Enchantment.DURABILITY,10).build());
                    return items;
                }
            }
            
            @Override
            public List<String> getInfo(final int level) {
                final List<String> lore = new ArrayList<String>();
                switch (level) {
                    case 1: {
                        lore.add(" §8▪ §7铁 剑");
                        lore.add("    §8▪ 耐久 X");
                        lore.add(" §8▪ §7弓");
                        lore.add("    §8▪ 耐久 X");
                        lore.add("    §8▪ 力量 I");
                        lore.add(" §8▪ §7铁 护腿");
                        lore.add("    §8▪ 耐久 X");
                        lore.add("    §8▪ 保护 I");
                        lore.add("    §8▪ 火焰保护 I");
                        lore.add(" §8▪ §7治疗药水 8❤");
                        lore.add(" §8▪ §7箭 §8x32");
                        lore.add(" §8▪ §7牛排");
                        break;
                    }
                    case 2: {
                        lore.add(" §8▪ §7铁 护腿");
                        lore.add("    §8▪ 耐久 X");
                        lore.add("    §8▪ 保护 I");
                        lore.add("    §8▪ 火焰保护 I");
                        lore.add("    §a+ 弹射物保护 I");
                        lore.add(" §a+ §7速度药水 II");
                        lore.add("    §8▪ 0:15");
                        lore.add(" §8▪ §7箭 §8x32 ➜ §ax56");
                        lore.add(" §8▪ §7牛排 §8x1 ➜ §ax2");
                        break;
                    }
                    case 3: {
                        lore.add(" §8▪ §7铁 §8➜ §a钻石 §7剑");
                        lore.add("    §8▪ 耐久 X");
                        lore.add(" §8▪ §7铁 护腿");
                        lore.add("    §8▪ 耐久 X");
                        lore.add("    §8▪ 保护 I ➜ §aII");
                        lore.add("    §8▪ 火焰保护 I ➜ §aII");
                        lore.add("    §a+ 弹射物保护 I");
                        lore.add(" §8▪ §7速度药水 §8x1 ➜ §ax2");
                        lore.add("    §8▪ 0:15");
                        lore.add(" §8▪ §7箭 §8x56 ➜ §ax64");
                        lore.add(" §8▪ §7箭 §8x56 ➜ §ax16");
                        break;
                    }
                    case 4: {
                        lore.add(" §8▪ §7弓");
                        lore.add("    §8▪ 耐久 X");
                        lore.add("    §8▪ 保护 I ➜ §aII");
                        lore.add(" §8▪ §7铁 护腿");
                        lore.add("    §8▪ 耐久 X");
                        lore.add("    §8▪ 保护 II");
                        lore.add("    §8▪ 火焰保护 II");
                        lore.add("    §a+ 弹射物保护 I ➜ §aII");
                        lore.add(" §8▪ §7治疗药水 8❤ §8x1 ➜ §ax2");
                        lore.add(" §8▪ §7箭 §8x64 ➜ §ax40");
                        lore.add(" §8▪ §7牛排 §8x2 ➜ §ax3");
                        break;
                    }
                    case 5: {
                        lore.add(" §8▪ §7铁 护腿");
                        lore.add("    §8▪ 耐久 X");
                        lore.add("    §8▪ 保护 II ➜ §aIII");
                        lore.add("    §8▪ 火焰保护 II");
                        lore.add("    §a+ 弹射物保护 II");
                        break;
                    }
                }
                return lore;
            }
        });
    }
    
    @Override
    public List<String> getInfo() {
        final List<String> lore = new ArrayList<String>();
        lore.add("§7你想要在战斗中占据上风.");
        lore.add("§7你喜欢宝藏.");
        lore.add("§7Yarr^_^.");
        return lore;
    }
    
    @Override
    public int unlockCost() {
        return 40000;
    }
    
    @Override
    public int energyMelee() {
        return 12;
    }
    
    @Override
    public int energyBow() {
        return 12;
    }
    
    @Override
    public String getSkillTip(final GamePlayer gamePlayer) {
        return this.mainSkill.getSkillTip(gamePlayer) + " " + this.thirdSkill.getSkillTip(gamePlayer);
    }
    
    @Override
    public void run() {
        GamePlayer.getOnlinePlayers().stream().filter(gamePlayer -> !gamePlayer.isSpectator() && ClassesManager.getSelected(gamePlayer).equals(this)).forEach(gamePlayer -> {
            if (Pirate.skill2Cooldown.getOrDefault(gamePlayer, (int)this.secondSkill.getAttribute(this.secondSkill.getPlayerLevel(gamePlayer))) > 0 && MegaWalls.getInstance().getGame().isWallsFall()) {
                Pirate.skill2Cooldown.put(gamePlayer, Pirate.skill2Cooldown.getOrDefault(gamePlayer, (int)this.secondSkill.getAttribute(this.secondSkill.getPlayerLevel(gamePlayer))) - 1);
            }
            if (Pirate.skill3Cooldown.getOrDefault(gamePlayer, 0) > 0) {
                Pirate.skill3Cooldown.put(gamePlayer, Pirate.skill3Cooldown.get(gamePlayer) - 1);
            }
            this.secondSkill.use(gamePlayer, gamePlayer.getPlayerStats().getKitStats(this));
            gamePlayer.sendActionBar(this.getSkillTip(gamePlayer));
        });
    }
    
    @EventHandler(priority = EventPriority.LOWEST)
    public void onEntityDamageByEntity(final EntityDamageByEntityEvent e) {
        if (e.isCancelled()) {
            return;
        }
        if (CitizensAPI.getNPCRegistry().isNPC(e.getEntity())) {
            return;
        }
        if (e.getEntity() instanceof Player && e.getDamager() instanceof Player) {
            final GamePlayer player = GamePlayer.get(e.getEntity().getUniqueId());
            if (player.isSpectator() || player.getGameTeam().isInTeam(GamePlayer.get(e.getDamager().getUniqueId()))) {
                return;
            }
            final Classes classes = ClassesManager.getSelected(player);
            if (classes.equals(this) && ((Player)e.getEntity()).getHealth() - e.getFinalDamage() < this.thirdSkill.getAttribute(player.getPlayerStats().getKitStats(classes).getSkill3Level())) {
                this.thirdSkill.use(player, player.getPlayerStats().getKitStats(classes));
            }
        }
    }
    
    @EventHandler(priority = EventPriority.LOWEST)
    public void onProjectileLaunch(final ProjectileLaunchEvent e) {
        if (e.isCancelled()) {
            return;
        }
        if (e.getEntity().getShooter() instanceof Player) {
            final GamePlayer shooter = GamePlayer.get(((Player)e.getEntity().getShooter()).getUniqueId());
            final Classes classes = ClassesManager.getSelected(shooter);
            if (classes.equals(this) && e.getEntity().getType() == EntityType.ARROW) {
                e.getEntity().setMetadata("Arrow", (MetadataValue)MegaWalls.getFixedMetadataValue());
            }
        }
    }
    
    static {
        skill2Cooldown = new HashMap<GamePlayer, Integer>();
        skill3Cooldown = new HashMap<GamePlayer, Integer>();
    }
}
